package com.e3.web.index.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.e3.core.base.ServiceException;
import com.e3.core.base.ServiceLocator;
import com.e3.core.base.SessionContext;
import com.e3.sysmg.security.model.ModuleModel;
import com.e3.sysmg.security.service.SecurityQueryService;
import com.e3.web.core.client.WebException;
import com.e3.web.core.client.WebUnkownException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.index.client.MainFrameService;
import com.e3.web.index.model.AccordionModel;
import com.e3.web.index.model.NavigatorModel;

public class MainFrameServiceImpl extends GwtRPCAction implements MainFrameService {

	private NavigatorModel nullModule = new NavigatorModel("-");
	
	private SecurityQueryService secQueryService;
	
	private SecurityQueryService getSecurityQueryService(){
		if(secQueryService==null){
			secQueryService = ServiceLocator.getQueryService(SecurityQueryService.class);
		}
		return secQueryService;
	}
	
	public List<NavigatorModel> getAllFunctionModules() throws WebException{
		try {
			List<ModuleModel> modules = getSecurityQueryService().getModulesByUserId(0);
			List<NavigatorModel> navModels = new ArrayList<NavigatorModel>(modules.size());
			Map<Long,NavigatorModel> navModelMap = new HashMap<Long,NavigatorModel>();
			for (ModuleModel moduleModel : modules) {
				Long id = moduleModel.getId();
				String code = moduleModel.getCode();
				//如果是超级管理员则显示模块管理，否则不显示
//				System.err.println("construct menu.........");
				if(!SessionContext.isSuperAdimin()&&"101001".equals(code)){
					continue;
				}
				Long pId = moduleModel.getParentId();
				NavigatorModel navModel = moduleToNavigatorModel(moduleModel);
				navModelMap.put(id, navModel);
				
				if(pId == null||pId == 0){
					navModels.add(navModel);
				}
				else {
					NavigatorModel pNavModel = navModelMap.get(pId);
					if(pNavModel!=null){
						pNavModel.addSubModel(navModel);
					}
				}
				
			}
			return navModels;
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new WebException(e.getMessage());
		} catch(Exception ex){
			ex.printStackTrace();
			throw new WebUnkownException();
		}
	}
	
	private NavigatorModel moduleToNavigatorModel(ModuleModel moduleModel){
		NavigatorModel navModel = new NavigatorModel();
		String name = moduleModel.getName();
		if("-".equals(name)){
			return nullModule;
		}
		navModel.setName(name);
		navModel.setCode(moduleModel.getCode());
		navModel.setPath(moduleModel.getPath());
		return navModel;
	}
	
	private AccordionModel moduleToAccordionModel(ModuleModel moduleModel){
		AccordionModel navModel = new AccordionModel();
		navModel.setName(moduleModel.getName());
		navModel.setCode(moduleModel.getCode());
		navModel.setPath(moduleModel.getPath());
		return navModel;
	}

	public List<NavigatorModel> getAllShortcutModules() {
		List<NavigatorModel> modules = new ArrayList<NavigatorModel>();
		NavigatorModel module = new NavigatorModel();
		module.setCode("901");
		module.setName("我的任务");
		module.setPath("");
		modules.add(module);
		
		module = new NavigatorModel();
		module.setCode("902");
		module.setName("短信");
		module.setPath("");
		modules.add(module);
		
		module = new NavigatorModel();
		module.setCode("903");
		module.setName("切换公司");
		module.setPath("");
		modules.add(module);
		
		module = new NavigatorModel();
		module.setCode("904");
		module.setName("我的信息");
		module.setPath("");
		modules.add(module);
		
		module = new NavigatorModel();
		module.setCode("909");
		module.setName("退出");
		module.setPath("");
		modules.add(module);
		
		return modules;
	}

	public List<AccordionModel> getAllAccordionModels() throws WebException {
		try {
			List<ModuleModel> modules = getSecurityQueryService()
					.getModulesByUserId(0);
			List<AccordionModel> accModels = new ArrayList<AccordionModel>(
					modules.size());

			Map<Long, AccordionModel> accModelMap = new HashMap<Long, AccordionModel>();
			for (ModuleModel moduleModel : modules) {
				Long id = moduleModel.getId();
				String code = moduleModel.getCode();
				// 如果是超级管理员则显示模块管理，否则不显示
				// System.err.println("construct menu.........");
				if (!SessionContext.isSuperAdimin() && "101001".equals(code)) {
					continue;
				}
				Long pId = moduleModel.getParentId();
				AccordionModel accModel = moduleToAccordionModel(moduleModel);
				accModelMap.put(id, accModel);

				if (pId == null || pId == 0) {
					accModels.add(accModel);
				} else {
					AccordionModel pAccModel = accModelMap.get(pId);
					if (pAccModel != null) {
						pAccModel.getChildren().add(accModel);
					}
				}

			}
			return accModels;
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new WebException(e.getMessage());
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new WebUnkownException();
		}
	}
	
//	private String encodePath(String path){
//		if(path==null)
//			return "";
//		//";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
//		path = path.replaceAll("\\;", "%3B");
//		path = path.replaceAll("\\/", "%2F");
//		path = path.replaceAll("\\?", "%3F");
//		path = path.replaceAll("\\:", "%3A");
//		path = path.replaceAll("\\@", "%40");
//		path = path.replaceAll("\\&", "%26");
//		path = path.replaceAll("\\=", "%5");
//		path = path.replaceAll("\\+", "%20");
//		path = path.replaceAll("\\$", "%24");
//		path = path.replaceAll("\\,", "%2C");
//		return path;
//	}
	
}
